package card

import (
	"go.mongodb.org/mongo-driver/bson"
	"math"
	"server/cfg"
	"server/db"
	carddb "server/game/card/db"
)

const (
	CardCollection = "t_card"
)

type CardDao struct {
	db *db.MongoMiddleware
}

func NewCardDao() *CardDao {
	return &CardDao{
		db: db.Db,
	}
}
func (c *CardDao) calculateCharm(card CardInfo) int64 {
	return card.Star * 10
}

// 计算卡牌的能力值，这里简单示例为等级 * 5
func (c *CardDao) calculateAbility(card CardInfo) float64 {
	return c.getTianFun(card)
}
func (c *CardDao) getJiChuNengLi(card CardInfo) float64 {
	config := cfg.ConfigData.TbCardBase.Get(int32(card.CTId))
	return (float64(config.Talent) + c.jiNengTianFu(card) + c.fuYeTianFu(card) + c.fuZhuangTianFu(card) + c.guangHuanTianFu(card)) * c.levelXiShu(card)

}
func (c *CardDao) getTianFun(card CardInfo) float64 {
	return c.getJiChuNengLi(card)*(1+c.geZhongtuJinJiaCheng()) + c.geZhongTuJinZengZhi()
}
func (c *CardDao) geZhongtuJinJiaCheng() float64 {
	return 0
}
func (c *CardDao) geZhongTuJinZengZhi() float64 {
	return 0
}
func (c *CardDao) fuZhuangTianFu(card CardInfo) float64 {
	return 0
}
func (c *CardDao) guangHuanTianFu(card CardInfo) float64 {
	return 0
}
func (c *CardDao) jiNengTianFu(card CardInfo) float64 {
	return 0
}
func (c *CardDao) fuYeTianFu(card CardInfo) float64 {
	return 0
}
func (c *CardDao) levelXiShu(card CardInfo) float64 {
	levelSquared := math.Pow(float64(card.Level), 2)
	// 计算 1.51 * (10^-6) * (card.Level^2)
	term1 := 1.51 * math.Pow(10, -6) * levelSquared
	// 计算 (2.17 * 10^-4) * card.Level
	term2 := 2.17 * math.Pow(10, -4) * float64(card.Level)
	// 计算 0.0316
	term3 := 0.0316
	// 计算括号内的和
	sum := term1 + term2 + term3
	// 最终结果
	result := 10000 * sum
	return result
}

func (c *CardDao) Insert(cardInfo CardInfo) error {
	return c.db.Insert(CardCollection, cardInfo)
}
func (c *CardDao) GetAll() ([]CardInfo, error) {
	filter := bson.M{}
	var cards []CardInfo
	err := db.Db.Find(carddb.CARDTABLENAME, filter, &cards)
	if err != nil {
		return nil, err
	}
	return cards, err
}

func (c *CardDao) cardInfoCtid(userId string, cardId int64) (CardInfo, error) {
	var card CardInfo
	filter := bson.M{"user_id": userId, "ct_id": cardId}
	err := c.db.FindOne(CardCollection, filter, &card)
	return card, err
}
func (c *CardDao) cardInfoCardId(userId string, cardId string) (CardInfo, error) {
	var card CardInfo
	filter := bson.M{"user_id": userId, "card_id": cardId}
	err := c.db.FindOne(CardCollection, filter, &card)
	return card, err
}
func (c *CardDao) cardList(userId string) ([]CardInfo, error) {
	var cardList []CardInfo
	filter := bson.M{"user_id": userId}
	err := c.db.Find(CardCollection, filter, &cardList)
	return cardList, err
}
func (c *CardDao) getCardInfoCarid(userId string, cardId string) (CardInfo, error) {
	var cardInfo CardInfo
	filter := bson.M{"user_id": userId, "card_id": cardId}
	err := c.db.FindOne(CardCollection, filter, &cardInfo)
	if err != nil {
		return cardInfo, err
	}
	return cardInfo, nil
}
func (c *CardDao) updateCardLevel(userId string, cardId int64, level int64) error {
	filter := bson.M{"user_id": userId, "ct_id": cardId}
	update := map[string]interface{}{
		"$set": map[string]interface{}{"level": level},
	}
	return c.db.Update(CardCollection, filter, update)
}
func (c *CardDao) updateCardStar(userId string, cardId int64, star int64) error {
	filter := bson.M{"user_id": userId, "ct_id": cardId}
	update := map[string]interface{}{
		"$set": map[string]interface{}{"star": star},
	}
	return c.db.Update(CardCollection, filter, update)
}
func (c *CardDao) updateCardSkill(userId string, cardId int64, key string, skill map[int32]int64) error {
	filter := bson.M{"user_id": userId, "ct_id": cardId}
	update := map[string]interface{}{
		"$set": map[string]interface{}{key: skill},
	}
	return c.db.Update(CardCollection, filter, update)
}
